/*
 * @Author: tanhu
 * @Date: 2024-05-24 17:39:29
 * @LastEditors: tanhu
 * @LastEditTime: 2024-06-25 16:10:12
 * @Description:
 */

import appConfig from './config';

// token验证
function tokenSing() {}
// 请求封装
const request = (options) => {
    return new Promise((resolve, reject) => {
        const BASE_URL = '';
        let token =
            uni.getStorageSync('token') ||
            'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6ImM0MTM5Mjg5LWVlNjAtNGUwOS05NThhLWM2OTZhYTNmMDkxNSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.y8plWXWMA4vw8WQL-r-NLfxJp15UXlRY8tEY1izNe0obfzQfPvszHG2UhdKDgrKOh_HDMjV9lN5wV-uPBZaQKw';
        const header = {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${token}`,
            ...options.header,
        };
        let urlWithParams = options.url;
        if (options.method == 'get' && options.params) {
            urlWithParams = `${options.url}?${Object.keys(options.params)
                .map(
                    (key) => `${key}=${encodeURIComponent(options.params[key])}`
                )
                .join('&')}`;
        }
        // let StartUrl = "http://10.0.0.129:8080";
        // let StartUrl ='http://10.0.0.129:31061/prod-api/'
        let StartUrl = appConfig.baseUrl;
        // #ifdef H5
        StartUrl = '/prod-api';
        // #endif
        uni.request({
            ...options,
            url: StartUrl + urlWithParams,
            header: header,
            success(res) {
                if (res.data.code == 500) {
                    uni.showToast({
                        title: res.data.msg || '数据异常',
                        icon: 'none',
                        duration: 1500,
                    });
                    reject(res.data);
                } else if (res.data.code == 401) {
                    uni.showToast({
                        title: res.data.msg || '数据异常',
                        icon: 'none',
                        duration: 1500,
                    });
                    uni.removeStorageSync('token');
                    if (
                        getCurrentPages()[getCurrentPages().length - 1].route ==
                        'pages/login/index'
                    ) {
                        return;
                    } else {
                        let pages = getCurrentPages();
                        let loginRoute = pages[pages.length - 1].$page.fullPath;
                        // let tempRouter ='';
                        // if(loginRoute.includes('?')){
                        //   tempRouter=loginRoute+'我的天啦';
                        // }
                        if (
                            !pages[pages.length - 1].$page.fullPath.includes(
                                '/pages/login/index'
                            )
                        ) {
                            // uni.reLaunch({
                            //   url: '/pages/login/index?router='+pages[pages.length-1].$page.fullPath,
                            // });
                            if (!uni.getStorageSync('logintoken')) {
                                uni.setStorageSync('logintoken', loginRoute);
                                uni.reLaunch({
                                    url: '/pages/login/index',
                                });
                            }
                        }
                    }
                    reject(res.data);
                } else if (res.data.code == 200 || res.data.code === 0) {
                    resolve(res.data);
                }
                if (urlWithParams.includes('Area')) {
                    resolve(res.data);
                }
                // uni.hideLoading();
            },
            fail: (err) => {
                // uni.hideLoading();
                console.log(err);
                reject(err);
            },
        });
    });
};
export default request;
